package com.longdaji.decoration.ui.wear;

import android.util.Log;

import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.bean.CaptainInfoBean;
import com.longdaji.decoration.data.bean.CartBean;
import com.longdaji.decoration.data.bean.GoodsAccountBean;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.data.network.HttpResponse;
import com.longdaji.decoration.utils.RxUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by longtao on 2017/12/19.
 */

public class WearPresenter extends RxPresenter<WearContract.View> implements WearContract.Presenter {
    private final static String TAG = WearPresenter.class.getSimpleName();
    private DataManager mDataManager;

    @Inject
    public WearPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }

    @Override
    public void getAccountData(String userId, List<CartBean.Cart> cartGoodsList) {
        List<String> idList = new ArrayList<>();
        for (int i = 0; i < cartGoodsList.size(); i++) {
            idList.add(cartGoodsList.get(i).getCartGoodsRecordId());
        }
        Log.d(TAG, "deleteChartGoods idList=" + idList.toString());
        addSubscribe(mDataManager.accountFromChart(userId, idList)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<GoodsAccountBean>handleResult())
                .subscribeWith(new CommonSubscriber<GoodsAccountBean>() {
                    @Override
                    public void onSuccess(GoodsAccountBean goodsAccountBean) {
                        if (mView != null) {
                            Log.d(TAG, "goodsAccountBean =" + goodsAccountBean);
                            mView.showGoodsAndReceiverData(goodsAccountBean);
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast(msg);
                        }
                    }
                }));
    }

    @Override
    public void getCaptainInfo(String dormitoryId, String dormitoryName) {
        addSubscribe(mDataManager.getCaptainInfo(dormitoryId, dormitoryName)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        if (mView != null) {
                            mView.showNoCaptain();
                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.toast(msg);
                        }
                    }
                }));
    }

    @Override
    public void queryCaptainInfo(String userId) {
        addSubscribe(mDataManager.queryCaptainInfo(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<List<CaptainInfoBean>>handleResult())
                .subscribeWith(new CommonSubscriber<List<CaptainInfoBean>>() {
                    @Override
                    public void onSuccess(List<CaptainInfoBean> captainInfoList) {
                        if (mView != null) {
                            if (captainInfoList == null || captainInfoList.size() <= 0) {
                                mView.showNoCaptain();
                            } else {
                                mView.showCaptainInfo(captainInfoList);
                            }

                        }
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "msg is " + msg);
                        if (mView != null) {
                            mView.showNoCaptain();
                        }
                    }
                }));
    }
}
